package com.dongbao.listeners;

import com.dongbao.data.Datacenter;
import com.intellij.execution.ExecutionListener;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.openapi.util.registry.RegistryValueListener;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MyExecutionListener  implements ExecutionListener, RegistryValueListener, ProjectManagerListener {

    private static final Logger LOG = Logger.getInstance(MyExecutionListener.class);
    private static final Key<List<ExecutionEnvironment>> ACTIVE_EXECUTIONS = Key.create("DebuggerFocusManager.active.executions");
    private final RegistryValue mySetting = Registry.get("debugger.mayBringFrameToFrontOnBreakpoint");
    private int mySessionCount;
    private boolean myFocusStealingEnabled;

    private MyExecutionListener() {
        Application application = ApplicationManager.getApplication();
        mySetting.addListener(this, application);
        application.getMessageBus().connect().subscribe(ProjectManager.TOPIC, this);
    }

    /**
     * 项目启动
     * */
    @Override
    public void processStartScheduled(@NotNull String executorId, @NotNull ExecutionEnvironment env) {
        LOG.info("调用了：[processStartScheduled]");
        /* 判断是否开启了共享服务 */
        if(Datacenter.sharedServices){
            /* 判断当前服务是否是DHG */

            /* 启动定时器定时监听启动的端口服务启动成功后添加到本机服务启动的集合中 */

        }



    }

    @Override
    public void processNotStarted(@NotNull String executorId, @NotNull ExecutionEnvironment env) {
        LOG.info("调用了：[processNotStarted]");
    }

    /**
     * 项目停止运行
     * */
    @Override
    public void processTerminated(@NotNull String executorId,
                                  @NotNull ExecutionEnvironment env,
                                  @NotNull ProcessHandler handler,
                                  int exitCode) {
        LOG.info("调用了：[processTerminated]");

        /* 判断当前服务是否是DHG */

        /* 将服务从启动的服务集合中删除 */


    }

    private void onDebugStarted(@NotNull ExecutionEnvironment env) {
        LOG.info("调用了：[onDebugStarted]");
    }

    private void onDebugFinished(@NotNull ExecutionEnvironment env) {
        LOG.info("调用了：[onDebugFinished]");
    }

    @Override
    public void afterValueChanged(@NotNull RegistryValue value) {
        LOG.info("调用了：[afterValueChanged]");
    }

    @Override
    public void projectClosed(@NotNull Project project) {
        LOG.info("调用了：[projectClosed]");
    }

    private synchronized void update(int sessionCountDelta) {
        LOG.info("调用了：[update]");
    }
}
